package com.ynet.middleground.riskmanage.service.impl;

import java.util.List;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.ynet.middleground.riskmanage.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.riskmanage.bean.*;
import com.ynet.middleground.riskmanage.model.ColdChainModel;
import com.ynet.middleground.riskmanage.service.ColdChainService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName ColdChainServiceImpl
 * @Description 对接怡之航冷链服务实现类
 * @Author zhangyongbin
 * @Date 2020/8/14 16:18
 * @Version 1.0
 */
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Api(value = "对接怡之航冷链服务实现类", tags = {"对接怡之航冷链服务实现类"})
@Path("coldchain")
@Service(timeout = 6000, version = "0.0.1")
public class ColdChainServiceImpl implements ColdChainService, GenericService {

    @Autowired
    ColdChainModel coldChainModel;

    @POST
    @Path("history")
    @ApiOperation(value = "历史业务量查询", notes = "历史业务量查询")
    @ApiImplicitParam(name = "requestObj", dataType = "ColdChainHistoryBusinessVolumeReq", paramType = "body",
        required = true)
    @Override
    public ServiceResult<List<ColdChainHistoryBusinessVolumeDTO>>
        getHistoryBusinessVolume(@Valid ColdChainHistoryBusinessVolumeReq req) {
        ServiceResult<List<ColdChainHistoryBusinessVolumeDTO>> result = new ServiceResult<>();
        try {
            result.setResult(coldChainModel.getHistoryBusinessVolume(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "历史业务量查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    @POST
    @Path("stock")
    @ApiOperation(value = "在库信息查询", notes = "在库信息查询")
    @ApiImplicitParam(name = "requestObj", dataType = "ColdChainStockInfoReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<ColdChainStockInfoDTO>> getStockInfo(@Valid ColdChainStockInfoReq req) {
        ServiceResult<List<ColdChainStockInfoDTO>> result = new ServiceResult<>();
        try {
            result.setResult(coldChainModel.getStockInfo(req));
            result.setPagerInfo(req.getPagerInfo());
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "在库查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * @author songyf
     * @Description 货种小类信息查询
     * @Date 2020-08-17 14:36
     * @Param ColdchainGoodsTypeQueryReqPager
     * @return ColdchainGoodsTypeDTO
     **/
    @POST
    @Path("goods-type")
    @ApiOperation(value = "货种小类信息查询", notes = "货种小类信息查询")
    @ApiImplicitParam(name = "requestObj", dataType = "ColdchainGoodsTypeQueryReqPager", paramType = "body",
        required = true)
    @Override
    public ServiceResult<List<ColdchainGoodsTypeDTO>>
        getColdchainGoodsType(@Valid ColdchainGoodsTypeQueryReqPager req) {
        ServiceResult<List<ColdchainGoodsTypeDTO>> result = new ServiceResult<>();
        try {
            result.setResult(coldChainModel.getColdchainGoodsType(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "货种小类信息查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        result.setPagerInfo(req.getPagerInfo());
        return result;
    }

    /**
     * @author songyf
     * @Description 货种小类信息同步
     * @Date 2020-08-17 16:36
     * @param requestObject
     * @return
     */
    @POST
    @Path("goods-type-syn")
    @ApiOperation(value = "货种小类信息同步（仅供冷链使用）", notes = "货种小类信息同步（仅供冷链使用）")
    @Override
    public ServiceResult<String> coldchainGoodsTypeSyn(@Valid ColdchainGoodsTypeReq requestObject) {
        ServiceResult<String> serviceResult = new ServiceResult<>();
        String returnsResult = null;
        try {
            returnsResult = coldChainModel.coldchainGoodsTypeSyn(requestObject);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "货种小类信息同步服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        serviceResult.setResult(returnsResult);
        return serviceResult;
    }

    /**
     * 查询冷链系统中现已动态质押的数据
     *
     * @param req 查询冷链动态质押数据请求信息
     * @return 动态质押数据集合
     * @author liulx
     * @date 2020-08-18 16:05:37
     */
    @POST
    @Path("dynamic-pledge")
    @ApiOperation(value = "冷链动态质押数据查询", notes = "查询冷链系统中现已动态质押的数据")
    @ApiImplicitParam(name = "requestObj", dataType = "ColdchainDynamicPledgeQueryReq", paramType = "body",
        required = true)
    @Override
    public ServiceResult<List<ColdchainDynamicPledgeDTO>> listDynamicPledge(@Valid ColdchainDynamicPledgeQueryReq req) {
        ServiceResult<List<ColdchainDynamicPledgeDTO>> result = new ServiceResult<>();
        try {
            result.setResult(coldChainModel.listDynamicPledge(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "冷链动态质押数据查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 用户查询服务申请
     *
     * @param req
     * @return
     */
    @POST
    @Path("customerApply")
    @ApiOperation(value = "用户查询服务申请",
        notes = "用户查询服务申请\n每次发起申请都按新申请去处理，不管是①审核拒绝重新上送、②等待审核期间重新上送、③新增货主账号，全都按照新申请逻辑处理，无需上送申请ID。", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "ColdchainCustomerApplyReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Object> customerApply(ColdchainCustomerApplyReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            result.setResult(coldChainModel.customerApply(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "用户查询服务申请服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "用户查询服务申请服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 申请结果查询接口
     *
     * @param req
     * @return
     */
    @POST
    @Path("applyResultQuery")
    @ApiOperation(value = "申请结果查询接口", notes = "申请结果查询接口", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "ApplyResultQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<ColdchainApplyResultDTO>> applyResultQuery(ApplyResultQueryReq req) {
        ServiceResult<List<ColdchainApplyResultDTO>> result = new ServiceResult<>();
        try {
            result.setResult(coldChainModel.applyResultQuery(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "用户查询服务申请服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "用户查询服务申请服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 接收冷链用户申请结果通知
     *
     * @param req
     * @return
     */
    @POST
    @Path("receiveApplyResult")
    @ApiOperation(value = "接收冷链用户申请结果通知", notes = "接收冷链用户申请结果通知", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "ReceiveApplyResultReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Object> receiveApplyResult(ReceiveApplyResultReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            result.setResult(coldChainModel.receiveApplyResult(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "接收冷链用户申请结果通知服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "接收冷链用户申请结果通知服务异常，请联系系统管理员");
        }
        return result;
    }

}
